package com.weiweiqin.controller.back;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weiweiqin.model.Account;
import com.weiweiqin.service.AccountService;
import com.weiweiqin.utils.Md5Util;
import com.weiweiqin.utils.RandomValidateCode;
import com.weiweiqin.vo.common.ModifyPasswordVO;
import com.weiweiqin.vo.common.Result;

@Controller
@RequestMapping("admin/login")
public class LoginController {

	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

	@Autowired
	private AccountService accountService;

	/**
	 * 用户更新pushNumber
	 * 
	 * @param requestBody
	 *            客户端传递过来的json字符串
	 * @return jsonStr
	 */
	@RequestMapping(value = "/login.do", method =  RequestMethod.POST)
	@ResponseBody
	public String login(String username, String password,String validateCode,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("Enter method login...,username=" + username + ":password="
				+ password);
		Result result = new Result();
		
		if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			result.setResult(false);
			return result.toJson();
		}
		
		Object code = request.getSession().getAttribute(RandomValidateCode.RANDLOGINCODEKEY);
//		if(null != code){
//			if(!code.toString().equalsIgnoreCase(validateCode)){
//				result.setResult(false);
//				result.setErrorMsg("请输入正确的验证码！");
//				return result.toJson();
//			}
//		}
		
		String ip = request.getRemoteAddr();
		logger.info("login ip="+ip);
		Account account = accountService.getByUsername(username);
		String md5Pwd = Md5Util.getMD5ofStr(password);
		System.out.println(md5Pwd);
		if (null != account && account.getPassword().equals(md5Pwd)) {
			result.setResult(true);
			// 设置session
			request.getSession().setAttribute("user", account);
			return result.toJson();
		}
		result.setResult(false);
		return result.toJson();
	}
	
	@RequestMapping(value = "/genCode.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void genValidateCode(HttpServletRequest request,
			HttpServletResponse response) {

		response.setContentType("image/jpeg");// 设置相应类型,告诉浏览器输出的内容为图片
		response.setHeader("Pragma", "No-cache");// 设置响应头信息，告诉浏览器不要缓存此内容
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expire", 0);
		RandomValidateCode randomValidateCode = new RandomValidateCode();
		try {
			// 输出图片方法
			randomValidateCode.getLoginRandcode(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 注销系统
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/logout.do", method = { RequestMethod.GET })
	@ResponseBody
	public String loginOut(HttpServletRequest request) {
		request.getSession().removeAttribute("user");
		Result result = new Result();
		result.setResult(true);
		return result.toJson();
	}

	/**
	 * 注销系统
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/modifyPassword.do", method = { RequestMethod.POST })
	@ResponseBody
	public String modifyPassword(ModifyPasswordVO modifyPasswordVO,
			HttpServletRequest request) {
		String username = null;
		if (null != request.getSession().getAttribute("username")) {
			username = request.getSession().getAttribute("username").toString();
		}
		Result result = new Result();
		Account account = accountService.getByUsername(username);
		if (null != account) {
			if (!modifyPasswordVO.getOldPassword()
					.equals(account.getPassword())) {
				result.setResult(false);
				result.setErrorMsg("原始密码不正确！");
				return result.toJson();
			}
			account.setPassword(modifyPasswordVO.getNewPassword());
			accountService.update(account);
		}
		result.setResult(true);
		return result.toJson();
	}
}
